/*
 *
 * Created on: 25.06.2012
 *
 * qdaemonapplication.h
 * This file is part of KOBUS
 * 
 * Copyright (C) 2012 - Ivan Penkin, MIPT
 * grek.penkin@gmail.com
 *
 * KOBUS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * KOBUS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with KOBUS; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 *  
 */

#ifndef QDAEMONAPPLICATION_H_
#define QDAEMONAPPLICATION_H_

#include <KobusCommon_global.h>

#include <qcoreapplication.h>
#include <qsocketnotifier.h>
#include <qdir.h>
#include <qfile.h>

#include <cstdlib>
#include <signal.h>

#include <sys/types.h>
#include <sys/socket.h>


namespace kobus {

class KOBUS_COMMON_EXPORT QDaemonApplication: public QCoreApplication
{
    Q_OBJECT

    Q_PROPERTY(QString userName READ userName WRITE setUserName)
    Q_PROPERTY(QString groupName READ groupName WRITE setGroupName)

    Q_PROPERTY(bool child READ child)


#if defined(Q_OS_UNIX)
  public:
    QDaemonApplication(int & /* argc */, char ** /* argv */);
    QDaemonApplication(
                        int & /* argc */, char ** /* argv */,
                        const QString & /* lockFileDir */
                      );
#else
  #error Platform is not supported!!!

  private:
    QDaemonApplication(int & /* argc */, char ** /* argv */);
    QDaemonApplication(
                        int & /* argc */, char ** /* argv */,
                        const QString & /* lockFileDir */
                      );
    QDaemonApplication(
                        int & /* argc */, char ** /* argv */,
                        const QString & /* lockFileDir */,
                        const QString & /* syslogName */
                      );
#endif

  public:
    virtual
    ~QDaemonApplication();

    static
    int exec();

    static
    QDaemonApplication * instance();

    bool setUserName(const QString & /* userName */);
    bool setGroupName(const QString & /* groupName */);

    const QString & userName() const;
    const QString & groupName() const;

    char const * userName_char() const;
    char const * groupName_char() const;

    const QFile & lockFile() const;

    bool child() const;

  private:
    QDaemonApplication();
    Q_DISABLE_COPY(QDaemonApplication)

    inline void initialize(char * /* arg0 */, const QString & /* lockFDir */);

    static inline void setSyncSignals();
    static inline void setSignals();
    static inline int setGidUid(QDaemonApplication * /* instance */);
    static inline int setSid();
    static inline int chDir();

    QFile & lockFile();

    QFile   d_lockFile;

    QString d_userName;
    QString d_groupName;

    const QByteArray d_userName_char;
    const QByteArray d_groupName_char;

    bool d_child;

    static int sigFdIn;
    static int sigFdOut;
    static int parentExitStatus;

    QSocketNotifier * sigNotifier;

  private slots:
    void handleSignal();

  signals:
    void intSignal();
    void quitSignal();
    void illSignal();
    void abrtSignal();
    void fpeSignal();
    void pipeSignal();
    void alrmSignal();
    void termSignal();
    void usr1Signal();
    void usr2Signal();
    void chldSignal();
    void contSignal();

    void termination();

  public:
    static void unixSignalHandler(int sigNo);
    static void parentsSigHandler(int sigNo);
};

} /* namespace config */

Q_DECLARE_TYPEINFO(kobus::QDaemonApplication, Q_COMPLEX_TYPE);

#endif /* QDAEMONAPPLICATION_H_ */
